package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name = "calendar")
@NamedQueries({
    @NamedQuery(name = "Calendar.findAll", query = "SELECT c FROM Calendar c"),
    @NamedQuery(name = "Calendar.findByCalendarId", query = "SELECT c FROM Calendar c WHERE c.calendarId = :calendarId"),
    @NamedQuery(name = "Calendar.findByName", query = "SELECT c FROM Calendar c WHERE c.name = :name"),
    @NamedQuery(name = "Calendar.findByScope", query = "SELECT c FROM Calendar c WHERE c.scope = :scope"),
    @NamedQuery(name = "Calendar.findByOwner", query = "SELECT c FROM Calendar c WHERE c.owner = :owner")})
public class Calendar implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @NotNull
    @Column(name = "calendarId")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer calendarId;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "name")
    private String name;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "scope")
    private String scope;

    @JoinTable(name = "contain", joinColumns = {
        @JoinColumn(name = "calendarId", referencedColumnName = "calendarId")}, inverseJoinColumns = {
        @JoinColumn(name = "eventId", referencedColumnName = "eventId")})
    @ManyToMany
    private List<Event> events = new ArrayList<>();

    @JoinColumn(name = "ownerUsername", referencedColumnName = "username")
    @ManyToOne(optional = false)
    private User owner;

    public Calendar() {
    }

    public Calendar(Integer calendarId) {
        this.calendarId = calendarId;
    }

    public Calendar(Integer calendarId, String name, String scope) {
        this.calendarId = calendarId;
        this.name = name;
        this.scope = scope;
    }

    public Integer getCalendarId() {
        return calendarId;
    }

    public void setCalendarId(Integer calendarId) {
        this.calendarId = calendarId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public List<Event> getEvents() {
        return events;
    }

    public void setEvents(List<Event> events) {
        this.events = events;
    }

    public void addEvent(Event event) {
        events.add(event);
    }

    public void removeEvent(Event event) {
        if (events != null) {
            events.remove(event);
        }
    }

    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }

    public boolean isPublic() {
        return CalendarScope.PUBLIC.toString().equalsIgnoreCase(scope);
    }

    public boolean isPrivate() {
        return CalendarScope.PRIVATE.toString().equalsIgnoreCase(scope);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (calendarId != null ? calendarId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Calendar)) {
            return false;
        }
        Calendar other = (Calendar) object;
        return Objects.equals(this.calendarId, other.calendarId);
    }

    @Override
    public String toString() {
        return "entity.Calendar[ calendarId=" + calendarId + " ]";
    }

}
